package com.engine.taskmask.persistence;

import com.engine.taskmask.sync.*;

import java.io.Serializable;
import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;


@PersistenceCapable
public class iTask implements Serializable{
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    @Persistent
    private String user_id;
    
    private IDate Rdate ;
    @Persistent
    private String Date_s;
    @Persistent
    private Date date;
    @Persistent
    private Boolean back;
    
    @Persistent
	private String taskTitle;
    @Persistent
   	private String taskNotes;
    @Persistent
   	private String taskLocation;
    @Persistent
	private String taskCategory;
    @Persistent
	private boolean taskFlagged;
    @Persistent
	private boolean taskProcessed;
    
    @Persistent
    private Integer taskUniqueIndex;
    @Persistent
    private Date update;
    @Persistent
    private Date syncdate;
    
    
    public iTask(IDate rdate, String taskTitle, String taskNotes,
			String taskLocation, String taskCategory, 
			boolean taskFlagged, boolean taskProcessed, User author) {
		
		this.Rdate = rdate;
		this.taskTitle = taskTitle;
		this.taskNotes = taskNotes;
		this.taskLocation = taskLocation;
		this.taskCategory = taskCategory;

		this.taskFlagged = taskFlagged;
		this.taskProcessed = taskProcessed;
		this.back=true;
		
		this.user_id=author.getUserId();
		if (this.Rdate!=null){
			this.Date_s=this.Rdate.toString();
			this.setDate();
	}
		else{
			this.Date_s=null;
			this.date=null;
		}
	}
    
    public iTask(IDate rdate, String taskTitle, String taskNotes,
			String taskLocation, String taskCategory,
			boolean taskFlagged, boolean taskProcessed) {
		
		this.Rdate = rdate;
		this.taskTitle = taskTitle;
		this.taskNotes = taskNotes;
		this.taskLocation = taskLocation;
		this.taskCategory = taskCategory;
		this.taskFlagged = taskFlagged;
		this.taskProcessed = taskProcessed;
		this.back= true;
		
		this.user_id=null;
		
		if (this.Rdate!=null){
			this.Date_s=this.Rdate.toString();
			this.setDate();
		}
			else{
				this.Date_s=null;
				this.date=null;
			}	
	}
    

    public iTask(IDate rdate, String taskTitle, String taskNotes,
			String taskLocation, String taskCategory, 
			boolean taskFlagged, boolean taskProcessed, String userID, Integer unique) {
		
		this.Rdate = rdate;
		this.taskTitle = taskTitle;
		this.taskNotes = taskNotes;
		this.taskLocation = taskLocation;
		this.taskCategory = taskCategory;
		
		this.taskFlagged = taskFlagged;
		this.taskProcessed = taskProcessed;
		this.back= true;
		
		this.taskUniqueIndex = unique;
		
		this.user_id=userID;
		if (this.Rdate!=null){
			this.Date_s=this.Rdate.toString();
			this.setDate();
		}
			else{
				this.Date_s=null;
				this.date=null;
			}
		
	}

	public iTask() {
		
		this.Rdate = null;
		this.taskTitle = null;
		this.taskNotes = null;
		this.taskLocation = null;
		this.taskCategory = null;
		
		this.taskFlagged = false;
		this.taskProcessed = false;
	
		this.back= true;
		this.Date_s=null;
		this.date=null;
	}
    
	public void setRdate(IDate rdate) {
		Rdate = rdate;
		setDate_s(rdate.toString());
		setDate();
	}
    
	public IDate getRdate() {
		  int year,day,month;
		  String Date_s=this.getDate_s();
		  
		  int index=Date_s.indexOf('/',0);
		  if (index !=-1){
		  day= Integer.parseInt(Date_s.substring(0,index));
		  int index2=Date_s.indexOf('/',index+1);
		  month=Integer.parseInt(Date_s.substring(index+1,index2));
		  year=Integer.parseInt(Date_s.substring(index2+1,Date_s.length()));
		  }
		  else {
			  index=Date_s.indexOf('/',0);
			  month= Integer.parseInt(Date_s.substring(0,index));
			  int index2=Date_s.indexOf('/',index+1);
			  day=Integer.parseInt(Date_s.substring(index+1,index2));
			  year=Integer.parseInt(Date_s.substring(index2+1,Date_s.length()));
			  
		  }
		  return new IDate(year, month, day);
	}
	
	private void setDate(){
		IDate d = getRdate();
		this.date=new java.util.Date(d.getYear(),d.getMonth()-1,d.getDay());
	}
	
	public Boolean getBack() {
		if (back!=null)
			return back;
		else
			return false;
	}

	public void setBack(Boolean back) {
		this.back = back;
	}

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public void setUserID(User author) {
		this.user_id=author.getEmail();
	}

	public String getUserID() {
		return this.user_id;
	}

	public void setDate_s(String date_s) {
		Date_s = date_s;
		setDate();
	}

	public String getDate_s() {
		return Date_s;
	}

	public String getTaskTitle() {
		return taskTitle;
	}

	public void setTaskTitle(String taskTitle) {
		this.taskTitle = taskTitle;
	}

	public String getTaskNotes() {
		return taskNotes;
	}

	public void setTaskNotes(String taskNotes) {
		this.taskNotes = taskNotes;
	}

	public String getTaskLocation() {
		return taskLocation;
	}

	public void setTaskLocation(String taskLocation) {
		this.taskLocation = taskLocation;
	}

	public String getTaskCategory() {
		return taskCategory;
	}

	public void setTaskCategory(String taskCategory) {
		this.taskCategory = taskCategory;
	}

	public boolean isTaskFlagged() {
		return taskFlagged;
	}

	public void setTaskFlagged(boolean taskFlagged) {
		this.taskFlagged = taskFlagged;
	}

	public boolean isTaskProcessed() {
		return taskProcessed;
	}

	public void setTaskProcessed(boolean taskProcessed) {
		this.taskProcessed = taskProcessed;
	}

	public Integer getTaskUniqueIndex() {
		return taskUniqueIndex;
	}

	public Date getUpdate() {
		return update;
	}

	protected void setUpdate(){
		Date date = new Date();
		setUpdate(date);
	}
	
	public void setUpdate(Date update) {
		this.update = update;
	}

	public Date getSyncdate() {
		return syncdate;
	}

	public void setSyncdate(){
		Date date = new Date();
		setSyncdate(date);
	}

	public void setSyncdate(Date syncdate) {
		this.syncdate = syncdate;
	}
	
	public Integer getUnique(){
		return taskUniqueIndex;
	}
	
	public void setUnique(int taskUniqueIndex){
		this.taskUniqueIndex=taskUniqueIndex;
	}
	
    @Override
	public boolean equals(Object r){
		if (taskUniqueIndex==null)
			return false;
		else
		return (this.getTaskUniqueIndex()==((iTask) r).getTaskUniqueIndex());
	} 
    
    public boolean containsField(String field){
		if (field==null)
			return false;
		if (field.equals(""))
			return false;
		if (this.taskTitle.toLowerCase().contains(field))
			return true;
		if (this.taskNotes.toLowerCase().contains(field.toLowerCase()))
			return true;
		if (this.Date_s.equals(field))
			return true;
		if (this.taskCategory.toLowerCase().equals(field.toLowerCase()))
			return true;
		return false;
	} 
    
	@Override
	public String toString() {
		return String
				.format("%s\t%s\t%s", Rdate, this.taskLocation, this.taskTitle);
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return super.hashCode();
	}

	public Date getDate() {
		return date;
	}

}
